package cn.simon.free.ratelimiter.config;

import cn.simon.free.ratelimiter.core.RateRedis;
import cn.simon.free.ratelimiter.utils.SpringRateLimitUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.Objects;

/**
 * redisson 懒加载初始化
 *
 * @author simon
 * @date 2020/3/25 17:36
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
public class RedissonConfig {

    private static RedissonClient redissonClient;

    private RedissonConfig() {

    }

    public static RedissonClient getRedissonInstance() {
        if (Objects.isNull(redissonClient)) {
            RateRedis bean = SpringRateLimitUtils.getApplicationContext().getBean(RateRedis.class);
            Config config = new Config();
            config.useSingleServer().setAddress(bean.getAddress()).setDatabase(bean.getDatabase()).setPassword(bean.getPassword());
            redissonClient = Redisson.create(config);
        }
        return redissonClient;
    }
}
